package dp.mobile.eden;

import java.util.ArrayList;

import org.w3c.dom.Node;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import dp.mobile.level.CampaignSelectLevelItem;
import dp.mobile.system.SoundManager;
import dp.mobile.system.Utilities;
import dp.mobile.system.XMLReader;

public class CampaignLevelSelectAct extends Activity implements OnClickListener {
	private static final String TAG = "Campaign Select Activity";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.campaign_level_select);
		
		mBackground 		= (ImageView) findViewById(R.id.background);
		mLevelInfo			= (RelativeLayout) findViewById(R.id.level_info);
		mLevelImage			= (ImageView) findViewById(R.id.level_image);
		mLevelDescription	= (TextView) findViewById(R.id.level_description);
		mOKButton			= (ImageView) findViewById(R.id.ok);
        mOKButton.setBackgroundResource(R.anim.ui_button);
		mOkAnimation = ((AnimationDrawable) mOKButton.getBackground());
		
		mLevelInfo.setOnClickListener(this);
		
		
		WindowManager wm 	= (WindowManager)this.getSystemService(Context.WINDOW_SERVICE);
        Display display 	= wm.getDefaultDisplay();
        
        mScaleX = display.getWidth() / (float)Utilities.SCREEN_WIDTH;
        mScaleY = display.getHeight() / (float)Utilities.SCREEN_HEIGHT;
		
		SharedPreferences pref = getSharedPreferences(Utilities.PREFERENCES, 0);
		mAvailableLevel = pref.getInt(Utilities.LAST_LEVEL_PREF, 1);
		
		switch (mAvailableLevel) {
		case 1 :
			mBackground.setBackgroundResource(R.drawable.campaign_0);
			break;
		case 2 :
			mBackground.setBackgroundResource(R.drawable.campaign_1);
			break;
		case 3 :
			mBackground.setBackgroundResource(R.drawable.campaign_2);
			break;
		case 4 :
			mBackground.setBackgroundResource(R.drawable.campaign_3);
			break;
		case 5 :
			mBackground.setBackgroundResource(R.drawable.campaign_4);
			break;
			
		}
		
		mLevels = new ArrayList<CampaignSelectLevelItem>();
		
		generateCampaignLevels();
	}
	
	private void generateCampaignLevels() {
		Node root = XMLReader.instance().parseXML(Utilities.CAMPAIGNLEVEL_FILE);
		
		int childCount = root.getChildNodes().getLength();
		
		for (int i = 0; i < childCount; ++i) {
			Node child = root.getChildNodes().item(i);
			
			if (child.getNodeType() == Node.TEXT_NODE) continue;
			
			if (child.getNodeName().equals("level"))
				mLevels.add(new CampaignSelectLevelItem(child));
		}
	}
	
	@Override
	public void onClick(View v) {
		if (v == mLevelInfo) {
			finish();
			Intent intent = new Intent(this, CutSceneAct.class);
			intent.putExtra("level", mLevels.get(mSelectedPosition).getLevelSource());
			startActivity(intent);
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			if (mLevelInfo.getVisibility() == View.GONE) {
				final float x = event.getX() / mScaleX;
				final float y = event.getY() / mScaleY;
				
				Log.d(TAG, "Pressed at" + x + " " + y);
				
				for (int i = 0; i < mAvailableLevel; ++i) {
					if (Utilities.calculateDistance(mLevels.get(i).getPosition().x, x, mLevels.get(i).getPosition().y, y) < 30) {
						pressedAt(i);
						break;
					}
				}
			} else {
				mLevelInfo.setVisibility(View.GONE);
			}
		}
		
		try {
			Thread.sleep(32);
		} catch (InterruptedException e) {
			mLevelInfo.setVisibility(View.GONE);
		}
		
		return true;
	}
	
	private void pressedAt(int pos) {
		SoundManager.instance(getBaseContext()).playSFX(Utilities.SFX_FOLDER + "/cursor.ogg");
		
		mLevelInfo.setVisibility(View.VISIBLE);
		mLevelDescription.setText(mLevels.get(pos).getDescription());
		mSelectedPosition = pos;
		mOkAnimation.start();
		//mLevelImage.setImageBitmap(bm);
	}
	
	@Override
	public void onBackPressed() {
		if (mLevelInfo.getVisibility() == View.VISIBLE)
			mLevelInfo.setVisibility(View.GONE);
		else
			super.onBackPressed();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		SoundManager.instance(getBaseContext()).temporarySetMute(true);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		SoundManager.instance(getBaseContext()).temporarySetMute(false);
	}
	
	private int mSelectedPosition;
	private float mScaleX, mScaleY;
	private RelativeLayout mLevelInfo;
	private ImageView mLevelImage;
	private ImageView mOKButton;
    private AnimationDrawable mOkAnimation;
	private TextView mLevelDescription;
	private ImageView mBackground;
	private ArrayList<CampaignSelectLevelItem> mLevels;
	private int	mAvailableLevel;
}
